﻿using System;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace MaterialEditor
{
    /// <summary>
    /// Loads and stores various fonts for rendering
    /// </summary>
    public static class Fonts
    {
        public static Dictionary<string, SpriteFont> fonts;

        /// <summary>
        /// Inilitize the class
        /// </summary>
        public static void Inilitize()
        {
            fonts = new Dictionary<string, SpriteFont>();
            ContentBuilder builder = Form1.instance.contentBuilder;

            DirectoryInfo info = new DirectoryInfo(Environment.CurrentDirectory + "\\Content");
            foreach (FileInfo file in info.GetFiles())
            {
                if (file.Extension == ".spritefont")
                {
                    // find the name for this file
                    string filename = file.Name.Split(new char[] { '.' })[0];
                    // build xna content
                    builder.Clear();
                    builder.Add(file.FullName, filename, "FontDescriptionImporter", "FontDescriptionProcessor");

                    // Build new data
                    string error_msg = builder.Build();
                    if (string.IsNullOrEmpty(error_msg))
                    {
                        // if there are no errors, load processed content and store its result
                        SpriteFont font = Form1.instance.content.Load<SpriteFont>(filename);
                        fonts.Add(filename, font);
                    }
                    else
                        System.Windows.Forms.MessageBox.Show(error_msg, "Content Build Error");
                }
            }
        }
    }
}